package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import util.TraductorXML;
import controller.CPR;
import dtos.ProveedorDTO;


public class AltaProveedor extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;
	private JLabel lblTitle;
	private JTextField txtRazSoc;
	private JRadioButton rdDesdeXml;
	private JRadioButton rdManual;
	private JButton cmdXMLSelector;
	private JTextField txtNroCuit;
	private JLabel lblNroCuit;
	private JLabel lblRazSoc;
	private JButton cmdAceptar;
	private JButton cmdVolver;
	private static AltaProveedor instance;
	private JFileChooser fcXML;
	private File file = null;
	
	private AltaProveedor() {
		super();
		initGUI();
		this.setLocationRelativeTo(null);
	}
	
	public static AltaProveedor getInstance()
	{
		if (instance == null)
			instance = new AltaProveedor();
		
		return instance;
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
			{
				lblTitle = new JLabel();
				getContentPane().add(lblTitle);
				lblTitle.setText("CPR - Proveedores - Nuevo Proveedor");
				lblTitle.setFont(new java.awt.Font("Calibri",0,20));
				lblTitle.setBounds(12, 12, 321, 32);
			}
			{
				cmdVolver = new JButton();
				getContentPane().add(cmdVolver);
				cmdVolver.setText("Volver");
				cmdVolver.setBounds(24, 319, 91, 31);
				cmdVolver.setFont(new java.awt.Font("Calibri",0,16));
				cmdVolver.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						cmdVolverActionPerformed(evt);
					}
				});
			}
			{
				ButtonGroup group = new ButtonGroup();
				group.add(getRdManual());
				group.add(getRdDesdeXml());
			}
			{
				cmdAceptar = new JButton();
				getContentPane().add(cmdAceptar);
				getContentPane().add(getLblRazSoc());
				getContentPane().add(getLblNroCuit());
				getContentPane().add(getTxtRazSoc());
				getContentPane().add(getTxtNroCuit());
				getContentPane().add(getCmdXMLSelector());
				getContentPane().add(getRdManual());
				getContentPane().add(getRdDesdeXml());
				cmdAceptar.setText("Aceptar");
				cmdAceptar.setBounds(483, 319, 91, 31);
				cmdAceptar.setFont(new java.awt.Font("Calibri",0,16));
				cmdAceptar.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent evt) {
							cmdAceptarActionPerformed(evt);
					}				
				});
			}
			pack();
			setSize(600, 400);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private JLabel getLblRazSoc() {
		if(lblRazSoc == null) {
			lblRazSoc = new JLabel();
			lblRazSoc.setText("Razon Social");
			lblRazSoc.setBounds(190, 139, 76, 14);
			lblRazSoc.setFont(new java.awt.Font("Calibri",0,14));
		}
		return lblRazSoc;
	}
	
	private JLabel getLblNroCuit() {
		if(lblNroCuit == null) {
			lblNroCuit = new JLabel();
			lblNroCuit.setText("Nro Cuit");
			lblNroCuit.setBounds(212, 176, 54, 14);
			lblNroCuit.setFont(new java.awt.Font("Calibri",0,14));
		}
		return lblNroCuit;
	}
	
	private JTextField getTxtRazSoc() {
		if(txtRazSoc == null) {
			txtRazSoc = new JTextField();
			txtRazSoc.setBounds(290, 136, 90, 20);
			txtRazSoc.setFont(new java.awt.Font("Calibri",0,14));
		}
		return txtRazSoc;
	}
	
	private JTextField getTxtNroCuit() {
		if(txtNroCuit == null) {
			txtNroCuit = new JTextField();
			txtNroCuit.setBounds(290, 173, 90, 20);
			txtNroCuit.setFont(new java.awt.Font("Calibri",0,14));
		}
		return txtNroCuit;
	}

	private JButton getCmdXMLSelector() {
		if(cmdXMLSelector == null) {
			cmdXMLSelector = new JButton();
			cmdXMLSelector.setText("Seleccionar Archivo...");
			cmdXMLSelector.setBounds(212, 217, 168, 23);
			cmdXMLSelector.setFont(new java.awt.Font("Calibri",0,14));
			cmdXMLSelector.setEnabled(false);
			cmdXMLSelector.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					cmdXMLSelectorActionPerformed(evt);
				}
			});
		}
		return cmdXMLSelector;
	}
	
	private JRadioButton getRdManual() {
		if(rdManual == null) {
			rdManual = new JRadioButton();
			rdManual.setText("Manual");
			rdManual.setBounds(34, 65, 80, 23);
			rdManual.setFont(new java.awt.Font("Calibri",0,14));
			rdManual.setSelected(true);
			rdManual.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					rdManualActionPerformed(evt);
				}
			});
		}
		return rdManual;
	}
	
	private JRadioButton getRdDesdeXml() {
		if(rdDesdeXml == null) {
			rdDesdeXml = new JRadioButton();
			rdDesdeXml.setText("Desde XML");
			rdDesdeXml.setBounds(116, 65, 106, 23);
			rdDesdeXml.setSelected(false);
			rdDesdeXml.setFont(new java.awt.Font("Calibri",0,14));
			rdDesdeXml.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					rdDesdeXmlActionPerformed(evt);
				}
			});
		}
		return rdDesdeXml;
	}
	
	private JFileChooser getFcXML() {
		if(fcXML == null) {
			fcXML = new JFileChooser();
		}
		return fcXML;
	}

	//Acciones
	//
	private void cmdXMLSelectorActionPerformed(ActionEvent evt) {
		int returnVal = getFcXML().showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
            this.file = getFcXML().getSelectedFile();
        } else {
        	this.file = null;
        }			
	}
	
	private void cmdVolverActionPerformed(ActionEvent evt) {
		this.dispose();
	}
	
	private void rdManualActionPerformed(ActionEvent evt)
	{
		if (!isManual())
			changeInputMethod();
	}
	
	private void rdDesdeXmlActionPerformed(ActionEvent evt)
	{
		if (isManual())
				changeInputMethod();
	}		
	
	private void cmdAceptarActionPerformed(ActionEvent evt)
	{
		boolean create = false;
		
		if (isManual())
		{
			String razSoc = txtRazSoc.getText();
			String nroCuit = txtNroCuit.getText();		
			
			if (! razSoc.equalsIgnoreCase("") && ! nroCuit.equalsIgnoreCase(""))
			{
				//try {
					create = CPR.getInstance().altaProveedor(razSoc, nroCuit);
					//} catch (RemoteException e) 
					//{}
			}
			else{
				JOptionPane.showMessageDialog(null, "Los campos no pueden estar vacios", "Error", JOptionPane.ERROR_MESSAGE);
				return;
			}
		}else
		{
			if (this.file != null && this.file.getName().contains("xml"))
			{
				//try {
					ProveedorDTO proveedor = TraductorXML.getInstance().xmlToProveedor(this.file);
					create = CPR.getInstance().altaProveedor(proveedor);
					//} catch (RemoteException e)
					//{}
			}
			else{
				JOptionPane.showMessageDialog(null, "Tiene que seleccionar el archivo XML", "Error", JOptionPane.ERROR_MESSAGE);
				return;
			}
		}
		
		if (!create)
			JOptionPane.showMessageDialog(null, "Ya existe el proveedor", "Error", JOptionPane.ERROR_MESSAGE);	
		else{
			JOptionPane.showMessageDialog(null, "Proveedor creado con Exito", "Confirm", JOptionPane.DEFAULT_OPTION);	
			this.file = null;
			txtNroCuit.setText("");
			txtRazSoc.setText("");
		}
	}
	
	//Metodos Privados
	//
	private void changeInputMethod()
	{
		txtNroCuit.setEnabled(!txtNroCuit.isEnabled());
		txtNroCuit.setText("");
		txtRazSoc.setEnabled(!txtRazSoc.isEnabled());
		txtRazSoc.setText("");
		cmdXMLSelector.setEnabled(!cmdXMLSelector.isEnabled());
	}
	
	private boolean isManual()
	{
		return txtNroCuit.isEnabled();		
	}
}
